<template>
    <div class="mainDiv">
        <div class="rowHead">
            <el-button-group>
                <el-button type="success" @click="ruku" icon="h-kaidan" v-qx="quanxian.ru">入库</el-button>
                <el-button type="success" @click="chuku" icon="h-kaidan" v-qx="quanxian.chu">出库</el-button>
            </el-button-group>
            <el-button type="info" @click="getList" style="float:right" icon="h-shuaxin1">刷新列表</el-button>
        </div>
        <el-row :gutter="20" class="rowHead">
            <el-col :span="16" id="el-col-16">
                <el-tag type="success">筛选：</el-tag>
                <el-date-picker v-model="page.times" type="daterange" align="right" placeholder="选择日期范围" :picker-options="pickerOptions2" @change="getList">
                </el-date-picker>
                <el-select v-model="page.zt" @change="getList" class="ml w200" v-qx="quanxian.status">
                    <el-option v-for="(o,index) in zhuanttai" :label="o.txt" :value="o.val" :key="index"></el-option>
                </el-select>
            </el-col>
            <el-col :span="8">
                <el-input style="width: 100%;" placeholder="搜索单号" @keyup.enter.native="getList" v-model="page.search"></el-input>
            </el-col>               
        </el-row>
        <el-table :data="list" :stripe="true" height="100%">
            <!-- <el-table-column prop="id" label="ID" width="80"></el-table-column> -->
            <el-table-column prop="order_sn" label="单号" width="180"></el-table-column>
            <el-table-column prop="ordertype" label="出入类型">
                <template slot-scope="scope">
                    <el-tag type="success" size="mini" v-if="scope.row.ordertype == 6">入库</el-tag>
                    <el-tag type="warning" size="mini" v-else>出库</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="addtime" label="开单日期" show-overflow-tooltip>
                <template slot-scope="scope">
                    {{scope.row.addtime|hourTime}}
                </template>
            </el-table-column>
            <el-table-column prop="aname" label="操作员"></el-table-column>
            <el-table-column prop="oname" label="审核员">
                <template slot-scope="scope">{{scope.row.oname?scope.row.oname:'暂无'}}</template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button-group v-if="scope.row.status==0">
                        <el-tooltip content="查看" placement="top" :open-delay="800" v-qx="quanxian.look">
                            <el-button icon="el-icon-h-chakan" type="info" size="mini" @click="chakan(scope.row.id)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="审核" placement="top" :open-delay="800" v-qx="quanxian.check">
                            <el-button icon="el-icon-h-check2" type="warning" size="mini" @click="shenhe(scope.row.id)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top" :open-delay="800" v-qx="quanxian.del">
                            <el-button icon="el-icon-delete" type="danger" size="mini" @click="shanchu(scope.row.id)"></el-button>
                        </el-tooltip>
                    </el-button-group>
                    <el-button-group v-else>
                        <el-tooltip content="查看" placement="top" :open-delay="800" v-qx="quanxian.look">
                            <el-button icon="el-icon-h-chakan" type="info" size="mini" @click="chakan(scope.row.id)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="反审核" placement="top" :open-delay="800" v-qx="quanxian.uncheck">
                            <el-button icon="el-icon-h-qiyong" type="warning" size="mini" @click="fanshenhe(scope.row.id)"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-sizes="[5,15, 30, 50, 100]" :page-size="page.limits" :current-page="page.pages" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'
import P from '@/service/shop/popup.js'

export default {
  name: 'stockList',
  data () {
    return {
      list: [],
      page: {
        pages: 1,
        limits: 15,
        search: '',
        zt: 0,
        times: [new Date(), new Date()]
      },
      total: 0,
      zhuanttai: [{
        txt: '未审核',
        val: 0
      }, {
        txt: '已审核',
        val: 1
      }],
      quanxian: {
        ru: 'stock_ru',
        chu: 'stock_chu',
        status: 'stock_status',
        check: 'stock_check',
        del: 'stock_del',
        look: 'stock_look',
        uncheck: 'stock_uncheck'
      },
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    ruku: async function () {
      let res = await C.openBrowserWindow('/addStockOrder', { ordertype: 6 }, { width: 1200, height: 880, useSpa: false, spaModel: 'shop', transparent: true, frame: false })
      if (res) {
        C.success(res.data.info)
        this.getList()
      }
    },
    chuku: async function () {
      let res = await C.openBrowserWindow('/addStockOrder', { ordertype: 7 }, { width: 1200, height: 880, useSpa: false, spaModel: 'shop', transparent: true, frame: false })
      if (res) {
        C.success(res.data.info)
        this.getList()
      }
    },
    // 审核
    shenhe: async function (id) {
      let res = await P.openStockPopup({ id: id })
      if (res) C.success(res.data.info)
      this.getList()
    },
    // 删除订单
    shanchu: function (id) {
      C.confirm('删除当前订单, 是否继续?', async () => {
        let res = await C.ajax('/shop/stock/deletestockorder', { 'id': id }, 'post')
        if (res) {
          C.success(res.data.info)
          this.getList()
        }
      })
    },
    // 查看当前订单
    chakan: async function (id) {
      await P.openStockPopup({ id: id })
      this.getList()
    },
    sizeChange: function (val) {
      this.page.limits = val
      this.getList()
    },
    currentChange: function (val) {
      this.page.pages = val
      this.getList()
    },
    getList: async function () {
      let url = '/shop/stock/orderlist'
      let data = await C.ajax(url, { page: JSON.stringify(this.page) }, 'post')
      if (data) {
        this.list = data.data
        this.total = data.count
      }
    },
    fanshenhe (id) {
      C.confirm('确定反审核该订单吗?', async () => {
        let res = await C.ajax('/shop/stock/unreviewstockorder', { 'id': id }, 'post')
        if (res) {
          C.success(res.data.info)
          this.getList()
        }
      })
    }
  },
  mounted: function () {
    this.getList()
  }
}
</script>